package com.lilihub.miaosha.controller;

import com.lilihub.miaosha.common.Result;
import com.lilihub.miaosha.domain.User;
import com.lilihub.miaosha.rabbitmq.MQSender;
import com.lilihub.miaosha.redis.UserKey;
import com.lilihub.miaosha.service.RedisService;
import com.lilihub.miaosha.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.ResultSet;

@Controller
@RequestMapping("/demo")
public class SampleController {



    @Autowired
    private UserService userService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private MQSender mqSender;


    @RequestMapping("/demo")
    public String thymeleaf(Model model){

        model.addAttribute("name", "lilihub");
        return "hello";
    }


    @RequestMapping("/db/get")
    @ResponseBody
    public Result dbget(){

        User user = userService.getById(1);

        return Result.success(user);
    }

    @RequestMapping("/db/tx")
    @ResponseBody
    public Result dbTx(){

        boolean isTx = userService.tx();

        return Result.success(isTx);
    }



    @RequestMapping("/redis/get")
    @ResponseBody
    public Result redisGet(){

        User user = redisService.get(UserKey.getById,"" + 1, User.class);

        return Result.success(user);
    }



    @RequestMapping("/redis/set")
    @ResponseBody
    public Result redisSet(){

        User user = new User(1,"dedede1");

        boolean isSet = redisService.set(UserKey.getById,"" + 1, user);  // "UserKey:id1"

        return Result.success(isSet);
    }

    @RequestMapping("/mq")
    @ResponseBody
    public Result testRabbit(){
        mqSender.send("hello rabbit");
        return Result.success("hello rabbit");
    }



    @RequestMapping("/mq/topic")
    @ResponseBody
    public Result testRabbitTopic(){
        mqSender.sendTopic("hello rabbit topic");
        return Result.success("hello rabbit");
    }


    @RequestMapping("/mq/fanout")
    @ResponseBody
    public Result testRabbitFanout(){
        mqSender.sendFanout("hello rabbit fanout");
        return Result.success("hello rabbit");
    }

    @RequestMapping("/mq/headers")
    @ResponseBody
    public Result testRabbitHeaders(){
        mqSender.sendHeaders("hello rabbit headers");
        return Result.success("hello rabbit");
    }


}
